//
//  PopoverPresentController.swift
//  zyqWeibo
//
//  Created by 张亚琼 on 15/6/28.
//  Copyright © 2015年 张亚琼. All rights reserved.
//

import UIKit
/// 专门用于视图控制器之间的转场，本身并不是视图控制器
class PopoverPresentController: UIPresentationController {
    
    // 展现的frame默认是0
    var presentFrame = CGRectZero
    
    // 懒加载遮罩视图(弹出转场视图之后，后面的原来的视图不能和用户交互，就遮盖起来)
    lazy var coverView: UIView = {
        let cover = UIView()
        cover.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        
        let tap = UITapGestureRecognizer(target: self, action: "close")
        cover.addGestureRecognizer(tap)
        return cover
        }()

    
    func close() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
        
    }
    
    /// 容器视图将要重新布局子视图
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        // 添加遮罩
        coverView.frame = containerView!.bounds
        // 把遮罩放在底层？？
        containerView?.insertSubview(coverView, atIndex: 0)
        
        // 设置被modal出来那个视图的frame
        presentedView()?.frame = CGRectMake(100, 56, 200, 240)
    }
}
